package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.CompanyHotValue;
import com.example.demo.mapper.CompanyHotValueMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/***
 ** Create by Wedream on 2021/7/16
 */
@RestController
@RequestMapping("/companyHotValue")
public class CompanyHotValueController {

    @Resource
    CompanyHotValueMapper companyHotValueMapper;

    @PostMapping  //增
    public Result<?> save(@RequestBody CompanyHotValue companyHotValue) {
        companyHotValueMapper.insert(companyHotValue);
        return Result.success();
    }

    @DeleteMapping("/{id}") //删
    public Result<?> delete(@PathVariable long id) {
        companyHotValueMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping //查
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "100") Integer pageSize,
                              @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<CompanyHotValue> wrapper = Wrappers.<CompanyHotValue>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
           wrapper.like(CompanyHotValue::getCompany,search);
        }
        Page<CompanyHotValue> companyHotValuePage = companyHotValueMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(companyHotValuePage);
    }


    @PutMapping //改
    public Result<?> update(@RequestBody CompanyHotValue companyHotValue) {
        companyHotValueMapper.updateById(companyHotValue);
        return Result.success();
    }
}
